package logic

import (
	"context"
	"usershop/good_srv/goods"

	"usershop/api/internal/svc"
	"usershop/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListGoodsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListGoodsLogic {
	return &ListGoodsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListGoodsLogic) ListGoods(req *types.ListGoodsRequest) (resp *types.ResMsg, err error) {
	// todo: 展示列表

	list, err := l.svcCtx.GoodsSrv.GoodsList(l.ctx, &goods.GoodsListRequest{
		Keyword: req.Keyword,
		Page:    req.Page,
		Size:    req.Size,
	})
	if err != nil {
		return &types.ResMsg{
			Cre:  -1,
			Msg:  "服务器内部错误",
			Data: nil,
		}, err
	}

	return &types.ResMsg{
		Cre: 0,
		Msg: "查询成功",
		Data: map[string]interface{}{
			"GoodsList": list.Item,
		},
	}, err
}
